﻿using System;
using System.Windows;
using System.Windows.Controls;
//using SomethingSpacialClassLib.Web;
using SomethingSpacial.Web;
using SomethingSpacial.ViewModel;
using SomethingSpacial.Model;


namespace SomethingSpacial.Views.BeamMeUp
{
    public partial class RoomJobs : Page
    {

        SomethingSpacialContext domainContext = new SomethingSpacialContext();

        public RoomJobs()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(LocationList_Loaded);
        }

        #region "Page Events"

        void LocationList_Loaded(object sender, RoutedEventArgs e)
        {
            ((LocationsViewModel)this.DataContext).loadLocations();
        }

        private void gridLocations_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            buttonSaveNew.Visibility = Visibility.Collapsed;
            buttonUpdate.Visibility = Visibility.Visible;
            buttonDelete.Visibility = Visibility.Visible;

            editDataForm.CurrentItem = gridLocations.SelectedItem;
            editDataForm.BeginEdit();
        }

        private void buttonUpdate_Click(object sender, RoutedEventArgs e)
        {
            editDataForm.CommitEdit();

            if (editDataForm.CurrentItem != null)
            {
               // LocationModel lm = editDataForm.CurrentItem as LocationModel;
                ((LocationsViewModel)this.DataContext).UpdateLocation();
            }
        }

        private void buttonAddNew_Click(object sender, RoutedEventArgs e)
        {

            buttonUpdate.Visibility = Visibility.Collapsed;
            buttonDelete.Visibility = Visibility.Collapsed;
            buttonSaveNew.Visibility = Visibility.Visible;

            Location NewLocation = new Location();

            NewLocation.LOC_Id = 0;
            NewLocation.LOC_Name = "New Location";
            NewLocation.LOC_City = "Miami";
            NewLocation.LOC_Lat = "000.00";
            NewLocation.LOC_Long = "-98.213";
            NewLocation.LOC_StreetAddress1 = "";
            NewLocation.LOC_Postalcode = "";

            editDataForm.CurrentItem = NewLocation;
            editDataForm.BeginEdit();
        }

        private void buttonSaveNew_Click(object sender, RoutedEventArgs e)
        {
            Button buttonCurrent = (Button)sender;
            String ButtonText = buttonCurrent.Content.ToString();
            editDataForm.CommitEdit();
            ((LocationsViewModel)this.DataContext).AddLocation((Location)editDataForm.CurrentItem);
            editDataForm.CurrentItem = null;
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            editDataForm.CommitEdit();
            ((LocationsViewModel)this.DataContext).DeleteLocation((Location)editDataForm.CurrentItem);
        }

        #endregion
    }
}
